package com.sxt.dao;

import com.sxt.common.PageInfo;
import com.sxt.pojo.User;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao{
    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    public User selectByNameAndPassword(String username,String password){
        String sql = "select id, username,password,realname,role,img from user where username = ? and password = ?";
        return super.selectOne(sql,User.class,username,password);//super调用父类方法
    }


    public PageInfo<User> selectPage(Map<String, String> params, Integer pageNo, Integer pageSize) {
        String sql = "select `id`, `username`, `password`, `realname`, `role`, `deleted`, `img`,create_time as createTime,deleted_time as deletedTime  from user where 1=1 ";
        //拼接动态sql
        if (params.containsKey("username")){
            sql = sql + " and username like '%"+params.get("username")+"%' ";
        }
        if (params.containsKey("realname")){
            sql = sql + " and realname like '%"+params.get("realname")+"%' ";
        }
        if (params.containsKey("role")){
            sql = sql + " and role="+params.get("role")+" ";
        }
        if (params.containsKey("deleted")){
            sql = sql + " and deleted="+params.get("deleted")+" ";
        }
        return  super.selectPage(sql,User.class,pageNo,pageSize);

    }

    /**
     * 根据ID 修改密码
     * @param id
     * @param password
     */
    public void updatePassword(String id, String password) {
        String sql = "update user set password = ?  where id = ? ";
        super.update(sql,password,id);
    }

    /**
     * 修改用户在职状态
     * @param id
     * @param deleted
     */
    public void delete(String id, Integer deleted) {
        String sql = "update user set deleted = ?,deleted_time=? where id = ? ";
        super.update(sql,deleted,new Date(),id);
    }

    /**
     * 根据用户查询用户
     * @param username
     * @return
     */
    public User selectByUsername(String username) {
        String  sql = "select id,username from user where username = ? ";
        return  super.selectOne(sql,User.class,username);
    }

    public void insert(String username, String password, String realname, String role, Integer deleted ) {
        String sql = "insert into user(username,password,realname,role,deleted) values (?,?,?,?,?)";
        super.update(sql,username,password,realname,role,deleted);
    }

    /**
     * 根据状态和角色查询用户
     * @param deleted
     * @param role
     * @return
     */
    public List<User> selectList(Integer deleted, Integer role) {
        String sql = "select id,realname from user where deleted = ? and role = ? ";
        return super.selectList(sql,User.class,deleted,role);
    }
}
